import json

from django.core.management.base import BaseCommand
from django.core.management import call_command
from stocksys import models as stocksys_models
from stocksys.management.commands.tasks_config import sys_tasks


class Command(BaseCommand):
    help = 'init_system'

    def add_arguments(self, parser):
        parser.add_argument('--with-clean',
                            action='store_true',
                            dest='delete',
                            default=False,
                            help='Delete exchange data')

    def handle(self, *args, **options):
        # --------------------------------------------------------------------------------------------------
        self.stdout.write(self.style.SUCCESS('清空交易所数据'))
        stocksys_models.ExchangeAddress.objects.all().delete()
        self.stdout.write(self.style.SUCCESS('载入交易所数据'))
        stock_address_name = ["上海", "深圳"]
        stock_sina_name = ["sh", "sz"]
        stock_jqdata_name = ["XSHG", "XSHE"]
        [stocksys_models.ExchangeAddress.objects.create(stock_address_name=stock_address_name[index],
                                                        stock_sina_name=stock_sina_name[index],
                                                        stock_jqdata_name=stock_jqdata_name[index],
                                                        ) for index in range(len(stock_address_name))]

        # --------------------------------------------------------------------------------------------------
        # 创建订单状态
        states = [0,1,2]
        state_names = ['未完成','完成','撤单']
        [stocksys_models.OrderState.objects.create(state=states[index],
                                                        state_name=state_names[index],
                                                        ) for index in range(len(states))]
        # --------------------------------------------------------------------------------------------------
        # 创建订单类型
        states = [0, 1]
        state_names = ['买入', '卖出']
        [stocksys_models.OrderType.objects.create(state=states[index],
                                                   state_name=state_names[index],
                                                   ) for index in range(len(states))]
        # --------------------------------------------------------------------------------------------------


        # --------------------------------------------------------------------------------------------------

        self.stdout.write(self.style.SUCCESS('清空系统配置数据'))
        stocksys_models.SysConf.objects.all().delete()

        self.stdout.write(self.style.SUCCESS('清空模型数据'))
        stocksys_models.ManagedCode.objects.all().delete()

        self.stdout.write(self.style.SUCCESS('清空模型数据'))
        stocksys_models.RuleInfo.objects.all().delete()

        self.stdout.write(self.style.SUCCESS('初始化系统配置数据'))
        sysconf = None
        try:
            user = stocksys_models.AUTH_USER.objects.get(id=1)
            try:
                sysconf = stocksys_models.SysConf.objects.get(user=user)
            except:
                self.stdout.write(self.style.SUCCESS('创建数据更新数据库'))
                self.stdout.write(self.style.SUCCESS('创建系统配置数据'))
                stocksys_models.SysConf.objects.create(user=user)
                sysconf = stocksys_models.SysConf.objects.get(user=user)
        except:
            raise Exception("没有创建初始用户")
        self.stdout.write(self.style.SUCCESS('创建初始任务'))

        tasks = sys_tasks()
        state_info = json.dumps({"state": "success",
                                 "current_schedule": 100,
                                 "info": "success",
                                 "color": "danger",
                                 })
        self.stdout.write(self.style.SUCCESS('创建任务数据'))
        for task in tasks:
            stocksys_models.DataUpdateTask.objects.create(sysconf=sysconf, task_name=task['name'], task_result=state_info)
        self.stdout.write(self.style.SUCCESS('创建初始规则和模型'))
        managed_code=stocksys_models.ManagedCode.objects.create(sysconf=sysconf, name="base_linear_regression")
        stocksys_models.ManagedCode.objects.create(sysconf=sysconf, name="base_bayesianridge_regression")
        stocksys_models.RuleInfo.objects.create(sysconf=sysconf, name="回归模型规则",name_en='base_mean_line',model_id=managed_code.id)

        # --------------------------------------------------------------------------------------------------

        self.stdout.write(self.style.SUCCESS('结束载入'))
